package com.yyys.web.utils;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Created by zhengsenwen on 2017/11/7.
 */
@Configuration
public class MyWebAppConfigure extends WebMvcConfigurerAdapter {
    private static Logger logger = Logger.getLogger(MyWebAppConfigure.class);

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        logger.info("Get Server relative path: " + System.getProperty("user.dir"));
        String temporaryPath = System.getProperty("user.dir").replaceAll("\\\\", "/");
        logger.info("After Change semantics: " + temporaryPath);

        //获取当前路径的上一级目录的路径
        String webRootAbsolutePath = temporaryPath.substring(0, temporaryPath.lastIndexOf("/"));
        logger.info("Return the parent directory" + webRootAbsolutePath);

        // 将资源路径映射到项目物理路径（计算机本地 D 盘）
        // registry.addResourceHandler("/api_files/**").addResourceLocations("file:D:/data/api_files");
        registry.addResourceHandler("/myResources/**").addResourceLocations("file:" + webRootAbsolutePath + "/myResources/");
        logger.info("outside picture resources" + "file:" + webRootAbsolutePath + "/myResources/");

        super.addResourceHandlers(registry);

    }

}
